/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_csk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/
using System;
using System.Reflection;

namespace CSharpKit.ComponentModel
{
    public static class AttributeHelper
    {
        public static V GetAttribute<U, V>(string field_name) where V : Attribute
        {
            var result = default(V);

            try
            {
                var typ = typeof(U);
                var fld = typ.GetField(field_name);
                var attrs = fld.GetCustomAttributes(false);

                for (var i = 0; i < attrs.Length; i++)
                {
                    var attr = attrs[i];
                    if (attr is V fsa)
                    {
                        result = fsa;
                        break;
                    }
                }
            }
            catch (System.Exception)
            {
                // TODO
            }

            return result;
        }

        public static V GetAttribute<U, V>(string field_name, BindingFlags bindingFlags) where V : Attribute
        {
            var result = default(V);

            try
            {
                var typ = typeof(U);
                var fld = typ.GetField(field_name, bindingFlags);
                var attrs = fld.GetCustomAttributes(false);

                for (var i = 0; i < attrs.Length; i++)
                {
                    var attr = attrs[i];
                    if (attr is V fsa)
                    {
                        result = fsa;
                        break;
                    }
                }
            }
            catch (System.Exception)
            {
                // TODO
            }

            return result;
        }




        public static string Alias<T>(string filed_name)
        {
            var attrcls = AttributeHelper.GetAttribute<T, AliasAttribute>(filed_name);
            // var type = typeof(T);
            // FieldInfo fi = type.GetField(filed_name);
            // var attrs = fi.GetCustomAttributes(typeof(AliasAttribute), false) as AliasAttribute[];
            // var vtemp = attrs?.Length > 0 ? attrs[0].Value : "";
            return "";
        }


        //}}@@@
    }


}
